﻿using MainView.Model;
using MainView.Model.sys;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace MainView.MyControl
{
    public partial class DevOper : UserControl
    {
        public RoomOper Ro { get; set; }

        string modeStr = "";

        Color offColor = Color.FromArgb(255, 77, 59);
        Color autoColor = Color.LimeGreen;
        Color handColor = Color.CadetBlue;

        Color selectColor = Color.FromArgb(255, 128, 128);
        Color nSelectColor = Color.FromArgb(0, 192, 192);

        public DevOper(RoomOper roomOper)
        {
            InitializeComponent();
            Ro = roomOper;
            UpShowFirst();
        }
        public void UpShowFirst()
        {
            Action DoAction = delegate ()
            {
                RoomNamelbl.Text = Ro.Name;
                UpShowOnline(false,0);
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }
        public void UpShowOnline(bool isonline,int mode)
        {
            Action DoAction = delegate ()
            {
                if (isonline == true)
                {
                    ucZM.Enabled = true;
                    ucAutoHand.Enabled = true;
                    UpShowMode(mode);
                    if (mode == 0)
                    {
                        ucAutoHand.Checked = false;
                        UnableAutoBtn();
                        lblHand.ForeColor = Color.LimeGreen;
                        lblAuto.ForeColor = Color.White;
                        panelHand.BackColor = Color.Transparent;
                        
                    }
                    else
                    {
                        ucAutoHand.Checked = true;
                        lblHand.ForeColor = Color.White;
                        lblAuto.ForeColor = Color.LimeGreen;
                        panelHand.BackColor = Color.Gray;
                        panelAuto.BackColor = Color.Transparent;

                    }
                }
                else
                {
                    ucZM.Enabled = false;
                    ucRollText1.Text = "设备离线！";
                    ucRollText1.ForeColor = Color.FromArgb(255, 77, 59);
                    UnableAutoBtn();
                    UnableHandBtn();
                    ucAutoHand.Enabled = false;
                }
               
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
            
        }


        private void UnableAutoBtn()
        {
            lblAuto.ForeColor = Color.White;

            btnMin.BackColor = Color.Gray;
            btnMid.BackColor = Color.Gray;
            btnMax.BackColor = Color.Gray;
            btnStable.BackColor = Color.Gray;

            btnMin.Enabled = false;
            btnMid.Enabled = false;
            btnMax.Enabled = false;
            btnStable.Enabled = false;

            panelAuto.BackColor = Color.Gray;
        }

        private void UnableHandBtn()
        {
            lblHand.ForeColor = Color.White;

            //ucSwitch1.Enabled = false;
            ucXD.Enabled = false;
            ucCY.Enabled = false;
            ucXF.Enabled = false;
            ucPF.Enabled = false;

            panelHand.BackColor = Color.Gray;
        }


        public void UpShowMode(int mode)
        {
            Action DoAction = delegate ()
            {
                if (mode == 0)
                {
                    lblHand.ForeColor = Color.LimeGreen;
                    lblAuto.ForeColor = Color.White;
                    panelHand.BackColor = Color.Transparent;
                    panelAuto.BackColor = Color.Gray;

                    ucRollText1.ForeColor = handColor;

                    ucXD.Enabled = true;
                    ucCY.Enabled = true;
                    ucXF.Enabled = true;
                    ucPF.Enabled = true;


                    modeStr = "手动模式运行中";
                }
                else 
                {
                    panelHand.BackColor = Color.Gray;
                    panelAuto.BackColor = Color.Transparent;
                    lblHand.ForeColor = Color.White;
                    lblAuto.ForeColor = Color.LimeGreen;
                    ucRollText1.ForeColor = autoColor;
                    btnMin.Enabled = true;
                    btnMid.Enabled = true;
                    btnMax.Enabled = true;
                    btnStable.Enabled = true;

                    if (mode == 1)
                    {                        
                        btnMin.BackColor = selectColor;
                        btnMid.BackColor = nSelectColor;
                        btnMax.BackColor = nSelectColor;
                        btnStable.BackColor = nSelectColor;
                        modeStr = "自动-能耗模式运行中";
                    }
                    else if (mode == 2)
                    {                        
                        btnMin.BackColor = nSelectColor;
                        btnMid.BackColor = selectColor;
                        btnMax.BackColor = nSelectColor;
                        btnStable.BackColor = nSelectColor;
                        modeStr = "自动-均衡模式运行中";
                    }
                    else if (mode == 3)
                    {                        
                        btnMin.BackColor = nSelectColor;
                        btnMid.BackColor = nSelectColor;
                        btnMax.BackColor = selectColor;
                        btnStable.BackColor = nSelectColor;
                        modeStr = "自动-强劲模式运行中";
                    }
                    else if (mode == 4)
                    {                      
                        btnMin.BackColor = nSelectColor;
                        btnMid.BackColor = nSelectColor;
                        btnMax.BackColor = nSelectColor;
                        btnStable.BackColor = selectColor;
                        modeStr = "自动-恒压模式运行中";
                    }
                }
                
                ucRollText1.Text = modeStr;
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);

        }

        private void btnMin_Click(object sender, EventArgs e)
        {
            UpShowMode(1);
        }

        private void btnMid_Click(object sender, EventArgs e)
        {
            UpShowMode(2);
        }

        private void btnMax_Click(object sender, EventArgs e)
        {
            UpShowMode(3);
        }

        private void btnStable_Click(object sender, EventArgs e)
        {
            UpShowMode(4);
        }

        private void ucAutoHand_CheckedChanged(object sender, EventArgs e)
        {
            if (ucAutoHand.Checked == true)
            {
                UpShowMode(1);
                UnableHandBtn();
            }
            else
            {
                UpShowMode(0);
                UnableAutoBtn();
            }
        }

        public void UpShowLight(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                ucZM.Checked = isOpen;               
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }
        public void UpShowUV(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                ucXD.Checked = isOpen;
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }
        public void UpShowO3(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                ucCY.Checked = isOpen;
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowFan(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                ucXF.Checked = isOpen;
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        public void UpShowFlue(bool isOpen)
        {
            Action DoAction = delegate ()
            {
                ucPF.Checked = isOpen;
            };
            ControlExtensions.UIThreadBeginInvoke(this, DoAction);
        }

        private void ucZM_Click(object sender, EventArgs e)
        {
            bool state = ucZM.Checked;
            if (Ro.ID == 0 || Ro.ID == 5)
            {
                Console.WriteLine(Ro.ID.ToString() + " " + state.ToString());
                MainForm.SetBits(Ro.ID, 2, state);
            }
            else if (Ro.ID == 3)
            {
                MainForm.SetBits(Ro.ID, 5, state);
            }
            else if (Ro.ID == 4)
            {
                MainForm.SetBits(Ro.ID, 0, state);
            }
        }

        private void ucXD_Click(object sender, EventArgs e)
        {
            bool state = ucXD.Checked;
            if (Ro.ID == 0 || Ro.ID == 5)
            {
                Console.WriteLine(Ro.ID.ToString() + " " + state.ToString());
                MainForm.SetBits(Ro.ID, 0, state);
            }
            else if (Ro.ID == 3)
            {
                MainForm.SetBits(Ro.ID, 6, state);
            }
            else if (Ro.ID == 4)
            {
                MainForm.SetBits(Ro.ID, 1, state);
            }
        }

        private void ucCY_Click(object sender, EventArgs e)
        {

        }

        private void ucXF_Click(object sender, EventArgs e)
        {
            bool state = ucXF.Checked;
            if (Ro.ID == 0 || Ro.ID == 5)
            {
                Console.WriteLine(Ro.ID.ToString() + " " + state.ToString());
                MainForm.SetBits(Ro.ID, 3, state);
            }
            else if (Ro.ID == 3)
            {
                MainForm.SetBits(Ro.ID, 0, state);
            }
            else if (Ro.ID == 4)
            {
                MainForm.SetBits(Ro.ID, 2, state);
            }
        }

        private void ucPF_Click(object sender, EventArgs e)
        {
            bool state = ucPF.Checked;
            if (Ro.ID == 0 || Ro.ID == 5)
            {
                Console.WriteLine(Ro.ID.ToString() + " " + state.ToString());
                //MainForm.SetBits(Ro.ID, 0, state);
            }
            else if (Ro.ID == 3)
            {
                MainForm.SetBits(Ro.ID, 3, state);
            }
            else if (Ro.ID == 4)
            {
                MainForm.SetBits(Ro.ID, 3, state);
            }
        }

        private void panelAuto_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
